package software;

import java.sql.*;
import java.util.*;

import software.Conn;

public class Fri_done {
	
	public  List <String> Fri_get_name(int Id){
		List <String>   list   =   new   ArrayList <String> ();
		String sql="{ call friends_getname(?) }";	
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1,Id);
			cs.execute();
			ResultSet rs=cs.getResultSet();
			while(rs.next()){
				list.add(rs.getString(1));
			}
			Dataconn.CloseCC();
			rs.close();
			return list; 		
			}catch(Exception e){
				e.printStackTrace();
				return  null;
			}
	}
	
	public  int get_friend_sum(int Id){
		int sum=0;
		String sql="{ call friend_sum(?,?) }";	
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1,Id);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			sum = cs.getInt(2);
			Dataconn.CloseCC();		
			}catch(Exception e){
				e.printStackTrace();
			}
			return sum; 
	}
	
	public void Fri_delete(int ID_User,int ID_Friend){
		String sql="{call friend_delete(?,?)}";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, ID_User);
			cs.setInt(2, ID_Friend);
			cs.execute();
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	
	public int Fri_getid(String name){
		int id=0;
		String sql="{call get_id(?,?)}";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, name);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			id=cs.getInt(2);
			Dataconn.CloseCC();	
			}catch(Exception e){
				e.printStackTrace();
			}	
			return id;
	}
	
	public int Fri_add(int ID_User,int ID_Friend,String Con_Request){
		int flag1=0;
		String sql="{call tem_friend_add(?,?,?,?)}";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, ID_User);
			cs.setInt(2, ID_Friend);
			cs.setString(3, Con_Request);
			cs.registerOutParameter(4,java.sql.Types.INTEGER);
			cs.execute();
			flag1=cs.getInt(4);
			Dataconn.CloseCC();	
			}catch(Exception e){
				e.printStackTrace();
			}	
			return flag1;
	}
	
	public int Fri_add_check(String user_name){
		int flag1=0;
		String sql="{call friend_add_check(?,?)}";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, user_name);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			flag1=cs.getInt(2);
			Dataconn.CloseCC();	
			}catch(Exception e){
				e.printStackTrace();
			}	
			return flag1;
	}
	
	public  List <String> Fri_add_user(int ID){
		List <String>   list   =   new   ArrayList <String> ();
		String sql="{ call friend_add_user(?) }";	
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1,ID);
			cs.execute();
			ResultSet rs=cs.getResultSet();
			while(rs.next()){
				list.add(rs.getString(1));
			}
			Dataconn.CloseCC();
			rs.close();
			return list; 		
			}catch(Exception e){
				e.printStackTrace();
				return  null;
			}
	}
	
	public void Fri_chuli_result(int ID_User,int ID_Friend,int flag){
		String sql="{call friend_chuli_result(?,?,?)}";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, ID_User);
			cs.setInt(2, ID_Friend);
			cs.setInt(3, flag);
			cs.execute();
			Dataconn.CloseCC();	
			}catch(Exception e){
				e.printStackTrace();
			}	
	}
	
}
